<?php
namespace haibao\erp\data;

use \haibao\frame\data\MysqlData;

abstract class BaseMysql extends MysqlData{
	private static $cacheObject = null;
	
	public function __construct($modelName){
		parent::__construct($modelName);
		$this->setConfig( \haibao\classlibrary\erp\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_ERP );
	}
	
	public function getCache($key){
		$cache = $this->getCacheObject();
		return $cache->get($key);
	}
	
	public function setCache($key,$value){
		$cache = $this->getCacheObject();
		$cache->set($key, $value);
	}
	
	public function unsetCache($key){
		$cache = $this->getCacheObject();
		$cache->del($key);
	}

	/**
	 * 记录日志到/tmp/下
	 */
	public function logResult($data){
	    if(PHP_OS == 'Linux'){
	        $fp = fopen('/tmp/sql.txt', "a");
	        flock($fp, LOCK_EX) ;
	        $line = "[" . date("Y-m-d H:i:s") . "]\t";
	        $line .= print_r($data, true);
	        $line .= "\n";
	        fwrite($fp, $line);
	        flock($fp, LOCK_UN);
	        fclose($fp);
	    }
	}
	
	/**
	 * @return \haibao\erp\cache\FileCache
	 */
	private function getCacheObject(){
		if (!self::$cacheObject){
			self::$cacheObject = new \haibao\erp\cache\FileCache();
		}
		return self::$cacheObject;
	}
}